#!/usr/bin/env python

# face database available at http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip
# unzip and enter the path below:
source = '../att_faces'

# number of different people
people = 2
# numer of templates per person
templates = 1
# number of different photos
photos = 5


from subprocess import call
call(["rm ./templates/* -rf"], shell=True)
call(["rm ./photos/* -rf"], shell=True)
from os import listdir, mkdir
from os.path import isfile, join
from random import choice,sample
from shutil import copyfile
from re import match
directories = [ dir for dir in listdir(source) if dir.startswith('s') and not isfile(source+'/'+dir) ]
directories = sample(directories,people)
used = []
for directory in directories:
    mkdir('templates/'+directory)
    files = [ file for file in listdir(source+'/'+directory+'/') if isfile(source+'/'+directory+'/'+file) and file.endswith('.pgm')]
    files = sample(files,templates)
    for file in files:
        used.append(directory+'/'+file)
        print './templates/'+directory+'/'+file
        copyfile(source+'/'+directory+'/'+file,'./templates/'+directory+'/'+file)

for i in xrange(photos):
    directory = choice(directories)
    files = [ file for file in listdir(source+'/'+directory+'/') if isfile(source+'/'+directory+'/'+file) and file.endswith('.pgm') and directory+'/'+file not in used ]
    file = choice(files)
    print './photos/'+file
    copyfile(source+'/'+directory+'/'+file,'./photos/'+directory+'_'+file)
